package com.smartwebee.android.blespp.application;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.smartwebee.android.blespp.hospital.MsgDetailActivity;
import com.tencent.smtt.sdk.QbSdk;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

import java.util.Map;

/**
 * Created by steward on 2018/7/31.
 */

public class MyApplication extends Application {
    private static final String TAG = "MyApplication";
    @Override
    public void onCreate() {
        super.onCreate();
        UMConfigure.init(this, "5b5ffc398f4a9d7a67000306", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "58e46a3eee96ff1dd62300e687ea018f");
        PushAgent mPushAgent = PushAgent.getInstance(this);
        mPushAgent.setResourcePackageName("com.smartwebee.android.blespp");

        mPushAgent.register(new IUmengRegisterCallback() {
            @Override
            public void onSuccess(String s) {
                Log.i(TAG + "token", s);
            }

            @Override
            public void onFailure(String s, String s1) {
            }
        });

        mPushAgent.setNotificationClickHandler(new UmengNotificationClickHandler() {
            @Override
            public void dealWithCustomAction(Context context, UMessage uMessage) {
                super.dealWithCustomAction(context, uMessage);
                String title = "";
                String content = "";
                for (Map.Entry<String, String> entry : uMessage.extra.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    if ("title".equals(key)) {
                        title = value;
                    }
                    if ("content".equals(key)) {
                        content = value;
                    }
                }
                Intent intent = new Intent(MyApplication.this, MsgDetailActivity.class);
                intent.putExtra("title", title);
                intent.putExtra("content", content);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        UmengMessageHandler messageHandler = new UmengMessageHandler() {
            @Override
            public void dealWithCustomMessage(final Context context, final UMessage msg) {
                new Handler().post(new Runnable() {

                    @Override
                    public void run() {
                    }
                });
            }
        };
        mPushAgent.setMessageHandler(messageHandler);
        initX5();
    }
    private void initX5() {
        //搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
        //x5内核初始化接口
        QbSdk.initX5Environment(getApplicationContext(), new QbSdk.PreInitCallback() {

            @Override
            public void onCoreInitFinished() {

            }

            @Override
            public void onViewInitFinished(boolean b) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                Log.d("app", " onViewInitFinished is $arg0");
            }
        });
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
